//
//  CountDownView.m
//  project
//
//  Created by 圆圆陈 on 2017/12/11.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "CountDownView.h"

@interface CountDownView ()
@property(nonatomic,strong)UIImageView *countView;
@property(nonatomic,strong)NSTimer* timer;
@end

@implementation CountDownView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.backgroundColor = [UIColor blackColor];
        _countView = [[UIImageView alloc]initWithFrame:CGRectMake((HHHWIDTH-50)/2,(HHHHEIGHT-50)/2-50, 50, 50)];
        _countView.hidden = YES;
        [_countView setImage:[UIImage imageNamed:@"3数字"]];
        [self addSubview:self.countView];
        
        [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:0.2 options:UIViewAnimationOptionLayoutSubviews animations:^{
            self.countView.frame =CGRectMake((HHHWIDTH-50)/2,(HHHHEIGHT-50)/2, 50, 50);
            self.timer =  [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(jianCe) userInfo:nil repeats:YES];
        } completion:^(BOOL finished) {
            self.countView.frame =CGRectMake((HHHWIDTH-50)/2,(HHHHEIGHT-50)/2-50, 50, 50);
            _countView.hidden = YES;
            [self.timer invalidate];
            self.timer = nil;
            [_countView setImage:[UIImage imageNamed:@"2数字"]];
            [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:0.2 options:UIViewAnimationOptionLayoutSubviews animations:^{
                self.countView.frame =CGRectMake((HHHWIDTH-50)/2,(HHHHEIGHT-50)/2, 50, 50);
                self.timer =  [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(jianCe) userInfo:nil repeats:YES];
            } completion:^(BOOL finished) {
                self.countView.frame =CGRectMake((HHHWIDTH-50)/2,(HHHHEIGHT-50)/2-50, 50, 50);
                _countView.hidden = YES;
                [self.timer invalidate];
                self.timer = nil;
                [_countView setImage:[UIImage imageNamed:@"1数字"]];
                [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:0.2 options:UIViewAnimationOptionLayoutSubviews animations:^{
                    self.countView.frame =CGRectMake((HHHWIDTH-50)/2,(HHHHEIGHT-50)/2, 50, 50);
                    self.timer =  [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(jianCe) userInfo:nil repeats:YES];
                } completion:^(BOOL finished) {
                    self.countView.frame =CGRectMake((HHHWIDTH-50)/2,(HHHHEIGHT-50)/2-50, 50, 50);
                    _countView.hidden = YES;
                    [self.timer invalidate];
                    self.timer = nil;
                    [_countView setImage:[UIImage imageNamed:@"Go开始"]];
                    [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:0.2 options:UIViewAnimationOptionLayoutSubviews animations:^{
                        self.countView.frame =CGRectMake((HHHWIDTH-50)/2,(HHHHEIGHT-50)/2, 50, 50);
                        self.timer =  [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(jianCe) userInfo:nil repeats:YES];
                    } completion:^(BOOL finished) {
                        self.countView.frame =CGRectMake((HHHWIDTH-50)/2,(HHHHEIGHT-50)/2-50, 50, 50);
                        _countView.hidden = YES;
                        [self.timer invalidate];
                        self.timer = nil;
                        [_countView setImage:[UIImage imageNamed:@"1"]];
                        [self removeFromSuperview];
                    }];
                }];
            }];
        }];
    }
    return self;
}
- (void)jianCe{
    CGRect rect = [[self.countView.layer presentationLayer ]frame];
    if (rect.origin.y>([UIScreen mainScreen].bounds.size.height-50)/2-25) {
        _countView.hidden = NO;
    }
}
@end
